package cn.school.web;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import cn.school.entity.Page_2;
import cn.school.entity.Student;
import cn.school.service.StudentService;
import cn.school.util.StringKit;

/**
 * Servlet implementation class StudentAjaxServlet
 */
@WebServlet("/studentAjaxServlet")
public class StudentAjaxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StudentAjaxServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	StudentService service = new StudentService();

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");

		String name = StringKit.convertNull(request.getParameter("sname"));
		String tel = StringKit.convertNull(request.getParameter("tel"));

		int pageNo = StringKit.toInt(request.getParameter("pageNo"), 1);
		Date start = StringKit.toDate(request.getParameter("datestart"));
		Date end = StringKit.toDate(request.getParameter("dateend"));
		// String start1 = StringKit.toString(start);
		// String end1 = StringKit.toString(end);

//		List<Student> list = service.search(pageNo, 20, name, tel, start, end);
//		int total = service.searchTotal(name, tel, start, end);
//		int pageTotal = total / 20 + (total % 20 == 0 ? 0 : 1);// 总页数
		Page_2<Student> p = service.paging(pageNo, name, tel, start, end);
		// String str = JSON.toJSONString(p, SerializerFeature.WriteMapNullValue);
		String str = JSON.toJSONStringWithDateFormat(p, "yyyy-MM-dd", SerializerFeature.WriteNullStringAsEmpty,
				SerializerFeature.WriteMapNullValue);
		response.getWriter().print(str);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
